package com.sixth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BottomArticleDao;
import com.sixth.entity.BottomArticle;
import com.sixth.entity.Subject;
import com.sixth.entity.TableResult;
import com.sixth.service.BannerService;
import com.sixth.service.BottomArticleService;
import com.sixth.until.CommonResult;
import com.sixth.until.OrderNoUtil;
import org.checkerframework.common.subtyping.qual.Bottom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: self-tui-education
 * @description:
 * @author: C
 * @create: 2022-04-23 19:30
 **/
@Service
public class BottomArticelmpl implements BottomArticleService {
@Autowired
private BottomArticleDao bottomArticleDao;
    @Override
    public TableResult selectAllArticle(Integer currentPage, Integer pageSize, BottomArticle bottomArticle) {
        PageHelper.startPage(currentPage,pageSize);
        List<BottomArticle> list = bottomArticleDao.selectBottomByCondition(bottomArticle.getName(),bottomArticle.getIsDisable());

        ArrayList<BottomArticle> list1=new ArrayList<>();

        for (BottomArticle bottoms:list) {
            if(bottoms.getParentId().equals("0")){
                list1.add(bottoms);
            }
        }

        for(BottomArticle one:list1){
            List<BottomArticle> children = findChildren(list, one.getId());
            one.setChildren(children);
        }

        PageInfo<BottomArticle> pageInfo = new PageInfo<BottomArticle>(list1);

        return new TableResult(2000,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }
    //查询对应菜单的子菜单 递归方法 一定要小心出现死循环
    public List<BottomArticle> findChildren(List<BottomArticle> list,String parentId){
        List<BottomArticle> children=new ArrayList<>();
        for(BottomArticle bottom:list){
            if(bottom.getParentId().equals(parentId)){
                children.add(bottom);
            }
        }

        for(BottomArticle c:children){
            List<BottomArticle> children1 = findChildren(list, c.getId());
            c.setChildren(children1);
        }
        return children;
    }



    @Override
    public CommonResult deleteFindById(BottomArticle bottomArticle) {
        String bottomArticleId=bottomArticle.getId();
        int row =bottomArticleDao.deleteByPrimaryKey(bottomArticleId);
        if (row>0){
            return new CommonResult(2000,"删除成功","null");
        }else {
            return new CommonResult(5000,"删除失败","null");
        }

    }

    @Override
    public CommonResult changeStatus(String id, Integer isDisable) {
        int i = bottomArticleDao.changeStatusByid(id,isDisable);
        bottomArticleDao.changeStatusByPid(id,isDisable);
        if (i != 0) {
            return new CommonResult(2000,"修改状态成功",null);
        }
        return new CommonResult(2000,"修改失败",null);
    }

    @Override
    public CommonResult addBottomArticle(BottomArticle bottomArticle) {
        String id1 = bottomArticle.getId();
        String parentId = bottomArticle.getParentId();
        String id = OrderNoUtil.getOrderNo();
        String name = bottomArticle.getName();
        Integer isDisable = bottomArticle.getIsDisable();
        Integer isDeleted = bottomArticle.getIsDisable();
        Date date = new Date();
        Date date1 = new Date();
        if (parentId == "0" && id1 == "0") {
           bottomArticleDao.addBottom(id, parentId, name, date, date1, isDisable, isDeleted, bottomArticle);
            return new CommonResult(2000, "添加成功", null);
        }
        parentId = id;
       bottomArticleDao.addBottom(id, parentId, name, date, date1, isDisable, isDeleted, bottomArticle);
        return new CommonResult(2000, "添加成功", null);

    }

    @Override
    public CommonResult UpdateBottom(BottomArticle bottomArticle) {
        String id = bottomArticle.getId();
        String name = bottomArticle.getName();
        System.out.println(id+"--------"+name);
        int row =bottomArticleDao.UpdateBottom(id,name);
        return new CommonResult(2000,"修改成功",null);
    }

}
